package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Strategy;
import cn.wolfcode.luowowo.article.domain.StrategyContent;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.StrategyTag;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.article.vo.StrategyStatisPersistenceVO;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.zookeeper.data.Id;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;

    @Autowired
    private StrategyContentMapper strategyContentMapper;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyTagService strategyTagService;

    @Override
    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyDetailMapper.selectForList(qo));
    }

    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public StrategyDetail get(Long id) {
        StrategyDetail strategyDetail = strategyDetailMapper.selectByPrimaryKey(id);
        if (strategyDetail != null) {
            StrategyContent content = strategyContentMapper.selectByPrimaryKey(id);
            strategyDetail.setStrategyContent(content);
        }
        return strategyDetail;
    }

    @Override
    public void saveOrUpdate(StrategyDetail strategyDetail, String tags) {
        StrategyContent content = strategyDetail.getStrategyContent();
        String text = content.getContent();
        if(text.length() > 100){
            strategyDetail.setSummary(text.substring(0,100));
        }else {
            strategyDetail.setSummary(text);
        }
        if (strategyDetail.getId() == null) {
            strategyDetail.setCreateTime(new Date());
            strategyDetailMapper.insert(strategyDetail);
            content.setId(strategyDetail.getId());
            strategyContentMapper.insert(content);
        } else {
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);
            content.setId(strategyDetail.getId());
            strategyContentMapper.updateByPrimaryKey(content);
            strategyDetailMapper.deleteRelation(strategyDetail.getId());
        }
        if (tags != null) {
            String[] tagArr = tags.split(",");
            for (String tag : tagArr) {
                StrategyTag strategyTag = strategyTagService.getByName(tag);
                if (strategyTag == null) {
                    strategyTag = new StrategyTag();
                    strategyTag.setName(tag);
                    strategyTagService.saveOrUpdate(strategyTag);
                }
                strategyDetailMapper.insertRelation(strategyDetail.getId(), strategyTag.getId());
            }
        }
    }

    @Override
    public void changeState(int state, Long id) {
        strategyDetailMapper.updateStateById(state, id);
    }

    @Override
    public List<StrategyDetail> getTopViewnum3ByDestId(Long destId) {
        return strategyDetailMapper.selectTopViewnum3ByDestId(destId);
    }

    @Override
    public void updateStatis(StrategyStatisPersistenceVO persistenceVO) {
        strategyDetailMapper.updateStatisById(persistenceVO);
    }

    @Override
    public List<StrategyDetail> getStrategyByUserId(Long userId) {
        return strategyDetailMapper.selectStrategyByUserId(userId);
    }

    @Override
    public void saveFavorToUser(Long sid, Long userId) {
        strategyDetailMapper.insertFavorRelation(sid, userId);
    }

    @Override
    public void deleteUserFavor(Long userId, Long sid) {
        strategyDetailMapper.deleteFavorRelation(userId, sid);
    }
}
